package com.annotation.hw.hw4;

import java.lang.reflect.Field;

/**
 * @author bin
 * @date 2025 - 03 -30
 **/
public class Util {
    public String generateSql(Class<?> clazz) throws IllegalAccessException {

        StringBuilder sb = new StringBuilder("select ");

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if(field.isAnnotationPresent(Colunm.class)){
                String columnName = field.getAnnotation(Colunm.class).value();
                sb.append(columnName).append(",");
            }else{
                sb.append(field.getName()).append(",");
                System.out.println(field.getName() + "此字段没有注解");
            }
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(" from ");
        if(clazz.isAnnotationPresent(table.class)){
            String tableName = clazz.getAnnotation(table.class).value();
            sb.append(tableName);
        }else {
            sb.append(clazz.getName());
            System.out.println(clazz.getName() + "此类没有注解");
        }

        return sb.toString();
    }
}
